package com.yipinhouse.signing.service.consumer;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yipinhouse.house.client.NewHouseQueryClient;
import com.yipinhouse.house.client.ResoldApartmentManageClient;
import com.yipinhouse.house.client.ResoldHouseManageClient;

import com.yipinhouse.signing.common.IntentionMoneyStatusEnum;

import com.yipinhouse.signing.infra.config.AlipayConfig;
import com.yipinhouse.signing.infra.model.IntentionMoney;
import com.yipinhouse.signing.service.IntentionMoneyService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;


@Component
@RocketMQMessageListener(topic = "yipinhouse-check-pay-status",consumerGroup = "yipinhouse-check-pay-status")
public class CheckPayStatusConsumer implements RocketMQListener<String> {


    @Resource
    private IntentionMoneyService intentionMoneyService;

    private ObjectMapper om = new ObjectMapper();
    @Override
    public void onMessage(String message) {
        System.out.println("执行支付回查");
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id,
                AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key,
                AlipayConfig.sign_type);
        AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();
        //请求接口
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + message + "\"," + "\"trade_no\":\"" + "" + "\"}");
        //转换格式
        String result = "";
        try {
            result = alipayClient.execute(alipayRequest).getBody();
            @SuppressWarnings("unchecked")
            Map<String, Map<String,String>> maps = om.readValue(result, Map.class);
            Map<String,String> date = maps.get("alipay_trade_query_response");
            String state = date.get("trade_status");
            String num = date.get("out_trade_no");
            if(state != null && state.equals("TRADE_SUCCESS")) {
                System.out.println("查询到订单支付成功");
                System.out.println("查看数据库是否匹配状态");
                IntentionMoney intentionMoney = intentionMoneyService.getOne(new QueryWrapper<IntentionMoney>()
                        .eq("intention_money_num",num));
                if(intentionMoney.getStatus().equals(IntentionMoneyStatusEnum.TO_BE_PAID.getCode())) {
                    System.out.println("数据库未执行成功业务，执行支付以后的逻辑补偿");
                    intentionMoneyService.payok(intentionMoney.getId());
                }else if (intentionMoney.getStatus().equals(IntentionMoneyStatusEnum.INVALID.getCode())){
                    System.out.println("订单失效，但用户已支付，执行退款逻辑");
                }
                else {
                    System.out.println("不处理");
                }
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
}
